package com.apps.ftapp.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.model.Question;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.Tools;

/**
 * Created by lyli on 2017/10/15.
 */

public class QuestionReportAdapter extends BaseAdapter {


    Context context;
    Question quesions;

    public QuestionReportAdapter(Context context, Question quesions) {
        this.context = context;
        this.quesions = quesions;
    }

    @Override
    public int getCount() {
        if (null != quesions.getAnswer() && quesions.getAnswer().size() > 0) {

            return quesions.getAnswer().size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return quesions.getAnswer().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //不同的item布局类型的数量
    @Override
    public int getViewTypeCount() {
        return 2;
    }

    //不同布局的item的类型
    @Override
    public int getItemViewType(int position) {
        L.i("main", "====" + quesions.getSizes() + "=====" + position);
        if (Tools.isNull(quesions.getAnswer().get(position).getId()).equals("")) {
            return 1;
        } else {
            return 0;
        }
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        switch (getItemViewType(position)) {
            case 0:
                ViewHolder holder;
                if (convertView == null) {
                    convertView = View.inflate(context, R.layout.item_answer_list, null);
                    holder = new ViewHolder(convertView);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }

                Question.Answer answer = quesions.getAnswer().get(position);
                holder.tvText.setText(answer.getAnswerMessage());
                holder.biao.setText(answer.getAnswerCode());

                //
                if (Tools.isNull(answer.getIsTrue()).equals("1")) {
                    holder.biao.setBackgroundResource(R.drawable.yuan1);
                } else {
                    holder.biao.setBackgroundResource(R.drawable.yuan);
                }
                break;

            case 1:
                QuestionViewHolder holder1;
                if (convertView == null) {
                    convertView = View.inflate(context, R.layout.my_error_detail_listview, null);
                    holder1 = new QuestionViewHolder(convertView);
                    convertView.setTag(holder1);
                } else {
                    holder1 = (QuestionViewHolder) convertView.getTag();
                }

                holder1.zhengquedaan.setText(quesions.getDaan());
                holder1.zuodabenti.setText(quesions.getPersonal().getDoCount() + "次");
                holder1.huidacuowu.setText(quesions.getPersonal().getWrongCount() + "次");
                holder1.huidazhengque.setText(quesions.getPersonal().getRightCount() + "次");


                holder1.beidati.setText(quesions.getAll().getDoCount() + "次");
                holder1.cuowulv.setText(quesions.getAll().getWrongRate());
                holder1.yixuanxiang.setText(quesions.getAll().getEasyWrong());

                holder1.jiexi.loadData(quesions.getJiexi(), "text/html; charset=UTF-8", null);
                holder1.jiexi.setBackgroundColor(0); // 设置背景色
                holder1.jiexi.getBackground().setAlpha(0); // 设置填充透明度 范围：0-255
                break;
        }


        //

        return convertView;
    }

    static class ViewHolder {

        TextView biao;
        TextView tvText;

        ViewHolder(View view) {
            tvText = view.findViewById(R.id.tv_answer);
            biao = view.findViewById(R.id.biao);
        }
    }


    static class QuestionViewHolder {

        TextView zhengquedaan;
        TextView zuodabenti;
        TextView huidazhengque;
        TextView huidacuowu;
        TextView beidati;
        TextView cuowulv;
        TextView yixuanxiang;

        WebView jiexi;


        QuestionViewHolder(View view) {
            zhengquedaan = view.findViewById(R.id.zhengquedaan);
            zuodabenti = view.findViewById(R.id.zuodabenti);
            huidazhengque = view.findViewById(R.id.huidazhengque);
            huidacuowu = view.findViewById(R.id.huidacuowu);
            beidati = view.findViewById(R.id.beidati);
            cuowulv = view.findViewById(R.id.cuowulv);
            yixuanxiang = view.findViewById(R.id.yixuanxiang);
            jiexi = view.findViewById(R.id.jiexi);

        }
    }
}
